package ui.panels;

import entities.Department;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.GenericDriver;
import manager.ManagerDriver;
import model.EditDepartmentTableModel;
import ui.windows.RemedySchedulerFrame;

/**
 *
 * @author viev
 */
public class DepartmentManagementPanel extends javax.swing.JPanel {

    /** Creates new form DepartmentManagementPanel */
    public DepartmentManagementPanel(RemedySchedulerFrame mainFrame) {
        this.mainFrame = mainFrame;
        genDriver = mainFrame.getGenericDriver();
        driver = (ManagerDriver) mainFrame.getDriver();
        initComponents();
        includeModel();
    }
    
    private void includeModel() {
        model = new EditDepartmentTableModel(genDriver.showDepartment());
        depTable.setModel(model);
        depTable.setFillsViewportHeight(true);
        depTable.getTableHeader().setReorderingAllowed(false);
        depTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        depTable.getTableHeader().setFont(new Font(null, Font.BOLD, 11));
        DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
        cellRenderer.setBackground(Color.yellow);
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setBackground(new Color(104,255,202));
        depTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        depTable.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
        depTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        depTable.getColumnModel().getColumn(1).setCellRenderer(cellRenderer1);
        depTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        depTable.getColumnModel().getColumn(2).setCellRenderer(cellRenderer1);
        depTable.getColumnModel().getColumn(3).setPreferredWidth(200);
        depTable.getColumnModel().getColumn(4).setPreferredWidth(70);
        
        createDepPanel = new CreateDepartmentPanel(mainFrame, this);
        editDepPanel = new EditDepartmentPanel(mainFrame, this);
        
        target = 1;
    }
    
    private void swithAction() {
        int choise = cbActions.getSelectedIndex();
        switchTarget(choise);
        cbActions.setSelectedIndex(0);
    }

    private void switchTarget(int choise) {
        if (target != choise || target == 4) {
            switch (choise) {
                case 0:
                    break;
                case 1:
                    this.remove(viewControl);
                    viewControl = new JScrollPane(viewDeps);
                    target = 1;
                    this.add(viewControl, BorderLayout.CENTER);
                    this.revalidate();
                    break;
                case 2:
                    this.remove(viewControl);
                    viewControl = new JScrollPane(createDepPanel);
                    target = 2;
                    this.add(viewControl, BorderLayout.CENTER);
                    this.revalidate();
                    break;
                case 3:
                    if (selectedDep != null) {
                        this.remove(viewControl);
                        editDepPanel.setSelectedDep(selectedDep);
                        viewControl = new JScrollPane(editDepPanel);
                        this.add(viewControl, BorderLayout.CENTER);
                        this.revalidate();
                        target = 3;
                    } else {
                        JOptionPane.showMessageDialog(null, "You migh select one department !");
                    }
                    break;
                case 4:
                    new Thread() {

                        @Override
                        public void run() {
                            deleteDepartment();
                        }
                    }.start();
                    break;
            }
        }
    }

    public void showAllDeps() {
        switchTarget(1);
    }

    public void showCreateDep() {
        switchTarget(2);
    }

    public boolean createDepartment(Department dep) {
        boolean returnVal = driver.insertDepartment(dep);
        if (returnVal) {
            dep.setModifiedDate(Calendar.getInstance().getTime());
            model.addElement(dep);
        }
        return returnVal;
    }

    public boolean updateDepartment(Department dep) {
        boolean returnVal = driver.updateDepartment(dep);
        if (returnVal) {
            dep.setModifiedDate(Calendar.getInstance().getTime());
            model.update(dep);
        }
        return returnVal;
    }

    private void deleteDepartment() {
        if (selectedDep != null) {
            target = 4;
            int choise = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this department !", "Delete", JOptionPane.YES_NO_OPTION);
            if (choise == JOptionPane.YES_OPTION) {
                boolean returnVal = driver.deleteDepartment(selectedDep);
                if (returnVal) {
                    model.remove(selectedDep);;
                    JOptionPane.showMessageDialog(this, "Deleted");
                } else {
                    JOptionPane.showMessageDialog(this, "Cannot delete !", "Error", JOptionPane.WARNING_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "You migh select one department !", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void setSelectedDep() {
        int index = depTable.getSelectedRow();
        if (index != -1) {
            selectedDep = model.getAt(index);
        } else {
            selectedDep = null;
        }
    }
    
    public void closeTarget() {
        switchTarget(1);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private GenericDriver genDriver;
    private ManagerDriver driver;
    private EditDepartmentTableModel model;
    private CreateDepartmentPanel createDepPanel;
    private EditDepartmentPanel editDepPanel;
    private Department selectedDep;
    private int target;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbActions = new javax.swing.JComboBox();
        viewControl = new javax.swing.JScrollPane();
        departmentM = new javax.swing.JPanel();
        viewDeps = new javax.swing.JScrollPane();
        depTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        cbActions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Actions", "View", "Add", "Edit", "Delete" }));
        cbActions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbActionsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(224, Short.MAX_VALUE)
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        departmentM.setBackground(new java.awt.Color(255, 255, 255));

        depTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        depTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                depTableMouseClicked(evt);
            }
        });
        viewDeps.setViewportView(depTable);

        javax.swing.GroupLayout departmentMLayout = new javax.swing.GroupLayout(departmentM);
        departmentM.setLayout(departmentMLayout);
        departmentMLayout.setHorizontalGroup(
            departmentMLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(departmentMLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(viewDeps, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
                .addContainerGap())
        );
        departmentMLayout.setVerticalGroup(
            departmentMLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(departmentMLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(viewDeps, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                .addContainerGap())
        );

        viewControl.setViewportView(departmentM);

        add(viewControl, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void cbActionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbActionsActionPerformed
    swithAction();
}//GEN-LAST:event_cbActionsActionPerformed

private void depTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_depTableMouseClicked
    setSelectedDep();
}//GEN-LAST:event_depTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbActions;
    private javax.swing.JTable depTable;
    private javax.swing.JPanel departmentM;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane viewControl;
    private javax.swing.JScrollPane viewDeps;
    // End of variables declaration//GEN-END:variables
}
